import tkinter as tk
import tkinter.messagebox
import os
from PIL import Image, ImageTk
import requests
#创建主窗口


#
#
#
#
# win = tk.Tk()
# win.title("图片爬虫工具")#这是窗口标题
# win.geometry("600x400+300+200")#600x400为窗口的宽高(即大小),300+200为窗口的显示位置

"""1.创建主窗口
2.这是窗口标题
3.将图片设置为窗口背景
"""
win = tk.Tk()
win.title("一个闹着玩的爬虫")
canvas = tk.Canvas(win, width=600, height=400, bd=0, highlightthickness=0)
impath='timg.jpg'
img= Image.open(impath)
photo = ImageTk.PhotoImage(img)
canvas.create_image(540, 400, image=photo)
canvas.pack()


root_path = os.getcwd() + "\images"

##设置主界面信息
tk.Label(win, text="图片地址：   ", font=("宋体", 15)).place(x=60, y=90, height=30, width=150)#x与y的数值为坐标轴
tk.Label(win, text="保存文件名：   ", font=("宋体", 15)).place(x=60, y=150,  height=30, width=150) #x与y的数值为坐标轴


##设置URL输入框
var_image_url = tk.StringVar()#设置url函数类型为字符串


#URL输入框默认信息
var_image_url.set('https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=705ffa145fda81cb51e685cd6267d0a4/4bed2e738bd4b31c5a30865b89d6277f9f2ff8c6.jpg')#设置输入框默认显示信息
entry_image_url = tk.Entry(win, textvariable=var_image_url)
entry_image_url.place(x=200, y=90,  height=30, width=150)


#自定义文件名输入框
var_file_name = tk.StringVar()
var_file_name.set("test.jpg")
entry_file_name = tk.Entry(win, textvariable=var_file_name)
entry_file_name.place(x=200, y=150,  height=30, width=150)


#定义爬虫函数
def start():
    url_info = var_image_url.get()
    print(url_info)
    url_file_geshi = tk.StringVar()
    header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    print(root_path)
    url_file_name = url_info.split("/")[-1]
    url_file_geshi = "." + url_info.split(".")[-1]
    if var_file_name:
        file_name =(entry_file_name.get() + url_file_geshi)
    else:
        file_name = url_file_name
    print(file_name)
    try:
        r = requests.get(url_info, headers=header)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        s_or_f = tk.messagebox.askyesno(title="恭喜",message="爬取成功是否保存？")
        if s_or_f:
            if not os.path.exists(root_path):
                os.mkdir(root_path)
            if not os.path.exists(file_name):
                    with open(file_name, "wb") as f:
                        f.write(r.content)
                        f.close()
                        def file_or_start():
                            def start_root_file():
                                os.system("start  explorer %s" % file_name )
                            tk.messagebox.showinfo(title="保存成功", message="是否立即打开文件", command=start_root_file)

                        tk.messagebox.showinfo(title="恭喜", message=file_name+"文件保存成功", command=file_or_start)
            else:
                    tk.messagebox.showerror(title="错误提示",message="文件已经存在,是否打开查看", command=file_or_start)

    except:
                    tk.messagebox.showerror(title="温馨提示",message="爬取失败")
#定义打开文件函数
def start_file():
    os.system('explorer.exe /n, ' + root_path)

#自定义保存指令函数的操作
def save_file():
    pass

def info():
    tk.messagebox.showinfo(title="温馨提示", message="emmmmmmm")


#功能设置按钮
bt_start_paqu = tk.Button(win, text="开始爬取", font=('宋体', 15), command=start).place(x=45, y=250)
#bt_save_file = tk.Button(win, text="立即保存", font=('宋体', 15), command=save_file).place(x=180, y=250)
bt_start_file = tk.Button(win, text="打开保存目录", font=('宋体', 15), command=start_file).place(x=200, y=250)

#创建退出按钮
bt_exit = tk.Button(win, text="退出", font=("宋体", 15),command=exit).place(x=500, y=250)


#创建作者信息
bt_info = tk.Button(win, text="By：罗启园", font=("宋体", 15), command=info).place(x=450, y=350)








#进入消息循环
win.mainloop()